from django.core.paginator import Paginator
from django.db import transaction
from django.http import JsonResponse
from django.shortcuts import render
from django.core.cache import cache
from apps.project.models import Projects_opinion_model
from apps.config.serializers import Menu_serializer

# 意见列表页面
def opinion_view(request):
    # 这里得获取其权限
    menu_list = cache.get('menu_list')
    menu_obj = Menu_serializer(menu_list, many=True)
    return render(request, 'opinion/list.html', {'data': menu_obj.data})

# 查询所有
def opinion_list(request):
    try:
        opinion_list = Projects_opinion_model.objects.filter()
        count = Projects_opinion_model.objects.count()
    except Exception as e:
        return JsonResponse({'code':201,'msg':'opinion模块查询失败，请联系管理员！'})
    list1 = []
    for i in opinion_list:
        dict = {}
        dict['id'] = i.id
        dict['opinion_name'] = i.opinion_name
        dict['remark_describe'] = i.remark_describe
        dict['create_time'] = i.create_time.strftime("%Y-%m-%d %H:%M:%S")
        list1.append(dict)
    pageIndex = request.GET.get('page')  # pageIndex = request.POST.get('pageIndex')
    pageSize = request.GET.get('limit')  # pageSize = request.POST.get('pageSize')
    pageInator = Paginator(list1, pageSize)
    contacts = pageInator.page(pageIndex)
    res = []
    for contact in contacts:
        res.append(contact)
    return JsonResponse({'code': 0, 'msg': '成功！', 'count': count, 'data': res})

# 添加页面
def opinion_insert_view(request):
    return render(request,'opinion/insert.html')

# 添加动作
@transaction.atomic
def opinion_insert(request):
    # 判断是否具有权限
    m_list = cache.get('menu_list')
    per_list = []
    for i in m_list:
        per_list.append(i.menu_code)
    if '50010301' not in per_list:
        return JsonResponse({'code': 201, 'msg': '您无此权限，请联系管理员！'})
    spoint_1 = transaction.savepoint()  # 可以设置多个保存点
    opinion_name = request.POST.get('opinion_name')
    remark = request.POST.get('remark_describe')
    try:
        Projects_opinion_model.objects.create(opinion_name=opinion_name, remark_describe=remark)
    except Exception as e:
        transaction.savepoint_rollback(spoint_1)  # 可以回滚到指定的保存点
        return JsonResponse({'code': 203, 'msg': 'opinion模块添加失败，请联系管理员！'})
    return JsonResponse({'code': 0, 'msg': '添加成功！'})

# 修改页面
def opinion_edit_view(request):
    return render(request,'opinion/edit.html')

# 修改动作
@transaction.atomic
def opinion_edit(request,id):
    # 判断是否有权限id
    m_list = cache.get('menu_list')
    per_list = []
    for i in m_list:
        per_list.append(i.menu_code)
    if '50010302' not in per_list:
        return JsonResponse({'code': 201, 'msg': '您无此权限，请联系管理员！'})
    spoint_1 = transaction.savepoint()  # 可以设置多个保存点
    name = request.POST.get('opinion_name')
    remark = request.POST.get('remark_describe')
    try:
        Projects_opinion_model.objects.filter(pk=id).update(opinion_name=name,remark_describe=remark)
    except Exception as e:
        transaction.savepoint_rollback(spoint_1)  # 可以回滚到指定的保存点
        return JsonResponse({'code': 203, 'msg': 'opinion模块修改出现错误，请联系管理员！'})
    return JsonResponse({'code': 0, 'msg': '修改成功！'})

# 删除
@transaction.atomic
def opinion_del(request,id):
    spoint_1 = transaction.savepoint()  # 可以设置多个保存点
    try:
        Projects_opinion_model.objects.get(pk=id).delete()
    except Exception as e:
        spoint_1 = transaction.savepoint()  # 可以设置多个保存点
        return JsonResponse({'code': 203, 'msg': 'opinion模块删除出现错误，请联系管理员！'})
    return JsonResponse({'code': 0, 'msg': '删除成功！'})